import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class ListPageView extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ListView.builder(itemCount: 50,
        itemExtent: 50,
        itemBuilder:
            (BuildContext context, int idx) =>
            ListTile(title: Text("Title $idx"), subtitle: Text("body $idx"))
    );
  }
}


class ListPageSeparatedView extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    print("DebugPrintStatement ListPageSeparatedView createState");

    return ListPageState();
  }
}

class ListPageState extends State<ListPageSeparatedView> {

  ScrollController _controller;
  bool isTop = false;

  @override
  void initState() {
    print("DebugPrintStatement initState");
    _controller = ScrollController();
    _controller.addListener(() {
      double offset = _controller.offset;
      print("DebugPrintStatement offset is $offset");

      if (_controller.offset > 1000) {

        // setState(() {
        //   isTop = true;
        // });
      } else if (_controller.offset < 300) {
        // setState(() {
        //   isTop = false;
        // });
      }
    });
    super.initState();
  }


  Widget build(BuildContext context) {
    return

    NotificationListener(
        onNotification: (scrollNotification) {//注册通知回调
      if (scrollNotification is ScrollStartNotification) {
        //滚动开始
        print('DebugPrintStatement Scroll Start');
      } else if (scrollNotification is ScrollUpdateNotification) {
          //滚动位置更新
        double offsetY = scrollNotification.metrics.pixels;

        print('DebugPrintStatement Scroll Update ${scrollNotification.scrollDelta}, offsetY: ${offsetY}');
          if(offsetY > 1000) {
            setState(() {
              isTop = true;
            });
          }else if(offsetY < 300) {
            setState(() {
              isTop = false;
            });
          }
      } else if (scrollNotification is ScrollEndNotification) {
          //滚动结束
          print('DebugPrintStatement Scroll End');
        }
      return false;
      }
    ,
  child:  Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RaisedButton(onPressed: (isTop ? ()
          {
            if (isTop) {
              _controller.animateTo(
                  0, duration: Duration(milliseconds: 200), curve: Curves.ease);
            }
          } : null), child: Text("Top")),
          Expanded(child: ListView.separated(itemBuilder: (BuildContext context, int idx) =>
              ListTile(title: Text("Title $idx"), subtitle: Text("body $idx")),
            separatorBuilder: (BuildContext context, int idx) =>
            (idx % 2 == 0 ? Divider(color: Colors.red) : Divider(color:Colors.green)),
            itemCount: 50,scrollDirection: Axis.vertical,controller: _controller,))]


    ));





  }

  @override
  void dispose() {
    _controller.dispose();
        super.dispose();
  }
}



